package vin.servlet.study;

 
import java.io.IOException;
import java.util.Date;
 

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletDemo5
 */
public class ServletDemo5 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletDemo5() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String data = "abcdefghijkmln";

		/**
		 * 设置数据合理的缓存时间值，以避免浏览器频繁向服务器发送请求，提升服务器的性能 这里是将数据的缓存时间设置为1天
		 */

		Date date = new Date();
		response.setDateHeader("Last-Modified", date.getTime());// Last-Modified:页面的最后生成时间
		response.setDateHeader("expires", System.currentTimeMillis() + 24 * 3600 * 1000);
		//response.setDateHeader("experice", System.currentTimeMillis() + 2*24 * 3600 * 1000);

		//Cache-Control来控制页面的缓存与否,public:浏览器和缓存服务器都可以缓存页面信息；
		response.setHeader("Cache-Control", "public");
		response.setHeader("Pragma", "Pragma"); //Pragma:设置页面是否缓存，为Pragma则缓存，no-cache则不缓存
		// TODO Auto-generated method stub

		response.getOutputStream().write(data.getBytes());

		//response.getWriter().write(data);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	@Override  
	  
	//复写了service中的getLastModified()方法  
	protected long getLastModified(HttpServletRequest req) {  
	   //获取文件  
//	   ServletContext context=this.getServletContext();  
//	   String path = context.getRealPath("/bbb.text");  
//	   File file=new File(path);  
	   return 1;
	} 

}
